{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c93a8226",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import requests\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fd8c4b8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp_html = requests.get('https://www.mycurrency.net')\n",
    "df_汇率表=pd.read_html(resp_html.content)[0]\n",
    "sr_汇率信息=df_汇率表.iloc[1:,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f2b101b",
   "metadata": {},
   "source": [
    "## 1. 实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2e314e70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['新币', '美元', '日元', '加币', '斯威士兰里兰吉尼', '阿根廷比索', '欧元', '英镑', '瑞士法郎', '澳币', '', '南非兰特', '阿富汗尼', '欧元', '阿尔及利亚第纳尔', '安哥拉宽扎', '东加勒比元', '亚美尼亚德拉姆', '阿鲁巴盾弗罗林', '阿塞拜疆马纳特', '巴哈马元', '巴林第纳尔', '孟加拉塔卡', '', '巴巴多斯元', '白俄罗斯卢布', '伯利兹元', '西非国家中央银行非洲法郎', '百慕大元', '玻利维亚比索', '波斯尼亚马克', '博茨瓦纳普拉', '巴西雷亚尔', '文莱元', '欧元', '布隆迪法郎', '柬埔寨瑞尔', '中非共同体法郎', '佛得角群岛埃斯库多', '开曼元', '智利比索', '哥伦比亚比索', '科摩罗法郎', '刚果法郎', '哥斯达黎加科朗', '欧元', '古巴披索', '捷克克朗', '丹麦克朗', '吉布提法郎', '多米尼加比索', '', '埃及镑', '美元', '厄立特里亚纳克法', '欧元', '埃塞俄比亚比尔', '', '丹麦克朗', '斐济元', '太平洋法郎', '冈比亚达拉西', '格鲁吉亚拉里', '加纳塞地', '直布罗陀镑', '丹麦克朗', '危地马拉格查尔', '英镑', '几内亚法郎', '圭亚那元', '海地古德', '洪都拉斯伦皮拉', '港币', '匈牙利福林', '冰岛克朗', '印度卢比', '印尼盾', '', '伊拉克第纳尔', '以色列谢克尔', '牙买加元', '约旦第纳尔', '哈萨克斯坦坚戈', '肯尼亚先令', '', '韩圆', '科威特第纳尔', '吉尔吉斯索姆', '老挝基普', '拉脱维亚拉特', '黎巴嫩镑', '莱索托洛蒂', '利比里亚元', '利比亚第纳尔', '欧元', '澳门元', '欧元', '马达加斯加阿里亚里', '马拉维克瓦查', '马币', '马尔代夫卢比', '毛里塔尼亚乌吉亚', '毛里求斯卢比', '墨西哥比索', '摩尔多瓦列伊', '蒙古图格里克', '摩洛哥迪拉姆', '莫桑比克梅蒂卡尔', '', '纳米比亚元', '尼泊尔卢比', '', '太平洋法郎', '新西兰元', '尼加拉瓜金科多巴', '尼日利亚奈拉', '挪威克朗', '阿曼里亚尔', '巴基斯坦卢比', '', '巴拿马巴波亚', '巴布亚基纳', '巴拉圭瓜拉尼', '秘鲁新索尔', '菲律宾比索', '波兰兹罗提', '卡塔尔里亚尔', '罗马尼亚列伊', '俄罗斯卢布', '卢旺达法郎', '', '圣多美和普林西比多布拉', '沙特里亚尔', '塞尔维亚第纳尔', '塞舌尔卢比', '塞拉利昂利昂', '所罗门群岛元', '', '斯里兰卡卢比', '', '苏里南盾', '瑞典克朗', '', '新台币', '塔吉克斯坦共和国索莫尼', '坦桑尼亚先令', '泰铢', '汤加潘加', '特立尼达和多巴哥元', '突尼斯第纳尔', '土耳其里拉', '土库曼斯坦斯坦马纳特', '乌干达先令', '乌克兰格里夫纳', '阿联酋迪拉姆', '乌拉圭比索', '乌兹别克斯坦斯坦索姆', '瓦努阿图瓦图', '委内瑞拉博利瓦', '越南盾', '太平洋法郎', '', '也门里亚尔', '赞比亚克瓦查'] 164\n"
     ]
    }
   ],
   "source": [
    "pattern_1 = re.compile('[\\u4e00-\\u9fa5]')\n",
    "lst_货币中文名=[]\n",
    "for 汇率信息_i in sr_汇率信息:\n",
    "    #print(汇率信息_i)\n",
    "    lst_中文字符=re.findall(pattern_1,汇率信息_i)\n",
    "    str_一个货币中文名=''.join(lst_中文字符)\n",
    "    lst_货币中文名.append(str_一个货币中文名)\n",
    "print(lst_货币中文名,len(lst_货币中文名))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3004c8ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.182, 0.14, 21.22, 0.197, 2.41, 198.64, 0.121, 0.105, 0.112, 0.217, 0.0, 2.46, 9.36, 11.69, 18.19, 128.72, 0.379, 53.46, 0.251, 0.239, 0.14, 0.053, 17.02, 0.0, 0.281, 751.22, 0.281, 79.23, 0.14, 0.968, 0.236, 1.98, 0.775, 0.181, 0.236, 415.47, 563.16, 79.23, 13.31, 0.116, 134.03, 543.08, 59.66, 331.88, 70.25, 0.91, 3.61, 2.94, 0.902, 24.88, 8.81, 330.76, 6.65, 1.22, 20.6, 1.96, 20.6, 0.105, 0.902, 0.32, 14.41, 10.11, 0.38, 1.71, 0.105, 0.902, 1.07, 0.105, 212.0, 29.24, 18.3, 3.67, 1.09, 47.14, 17.1, 12.46, 330.76, 904.25, 183.06, 0.459, 22.47, 0.1, 75.22, 18.05, 126.33, 200.66, 0.043, 12.28, 31.48, 0.085, 514.87, 2.41, 25.5, 0.76, 0.414, 1.12, 7.44, 627.91, 242.33, 0.593, 2.15, 50.11, 6.39, 2.61, 2.37, 505.02, 1.28, 8.97, 294.7, 2.41, 19.82, 0.251, 14.41, 0.245, 5.14, 205.32, 1.42, 0.054, 39.58, 0.1, 0.14, 0.596, 980.51, 0.479, 8.19, 0.514, 0.511, 0.615, 11.34, 202.75, 0.251, 127.67, 0.525, 14.15, 2.08, 943.46, 1.16, 79.87, 42.29, 84.43, 5.46, 1.33, 825.07, 4.31, 1.29, 343.08, 4.57, 0.338, 0.949, 0.411, 5.86, 0.491, 479.03, 5.82, 0.516, 5.62, 694.15, 16.81, 879.91, 697.31, 14.41, 1.28, 33.54, 263.49] 164\n"
     ]
    }
   ],
   "source": [
    "lst_汇率=[]\n",
    "for 汇率信息_i in sr_汇率信息:\n",
    "    pattern_2 = re.compile(r'\\d+\\.\\d+')\n",
    "    lst_汇率_i=re.findall(pattern_2 ,汇率信息_i)\n",
    "    if len(lst_汇率_i)==1:\n",
    "        float_汇率_i=float(lst_汇率_i[0])\n",
    "    else:\n",
    "        float_汇率_i=0.0\n",
    "    lst_汇率.append(float_汇率_i)\n",
    "print(lst_汇率,len(lst_汇率))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e8a949eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Singapore', 'UnitedStates', 'Japan', 'Canada', 'Swaziland', 'Argentina', 'EuropeUnion', 'UnitedKingdom', 'Switzerland', 'Australian', 'adsbygooglewindowadsbygooglep', 'SouthAfrica', 'Afghanistan', 'Albania', 'Algeria', 'Angola', 'Anguilla', 'Armenia', 'Aruba', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'adsbygooglewindowadsbygooglep', 'Barbados', 'Belarus', 'Belize', 'Benin', 'Bermuda', 'Bolivia', 'BosniaandHerzegovina', 'Botswana', 'Brazil', 'BruneiDarussalam', 'Bulgaria', 'Burundi', 'Cambodia', 'Cameroon', 'CapeVerde', 'CaymanIslands', 'Chile', 'Colombia', 'Comoros', 'CongoKinshasa', 'CostaRica', 'CroatiaHrvatska', 'Cuba', 'CzechRepublic', 'Denmark', 'Djibouti', 'DominicanRepublic', 'EastTimor', 'Egypt', 'ElSalvador', 'Eritrea', 'Estonia', 'Ethiopia', 'FalklandIslandsMalvinas', 'FaroeIslands', 'Fiji', 'FrenchPolynesia', 'Gambia', 'Georgia', 'Ghana', 'Gibraltar', 'Greenland', 'Guatemala', 'Guernsey', 'Guinea', 'Guyana', 'Haiti', 'Honduras', 'HongKong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'IranIslamicRepublicof', 'Iraq', 'Israel', 'Jamaica', 'Jordan', 'Kazakhstan', 'Kenya', 'KoreaNorth', 'KoreaSouth', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'LibyanArabJamahiriya', 'Lithuania', 'MacaoSAR', 'Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mauritania', 'Mauritius', 'Mexico', 'MoldovaRepublicof', 'Mongolia', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nepal', 'NetherlandsAntilles', 'NewCaledonia', 'NewZealand', 'Nicaragua', 'Nigeria', 'Norway', 'Oman', 'Pakistan', 'PalestinianTerritory', 'Panama', 'PapuaNewGuinea', 'Paraguay', 'Peru', 'Philippines', 'Poland', 'Qatar', 'Romania', 'RussianFederation', 'Rwanda', 'SaintMartinFrenchpart', 'SaoTomeandPrincipe', 'SaudiArabia', 'Serbia', 'Seychelles', 'SierraLeone', 'SolomonIslands', 'Somalia', 'SriLanka', 'Sudan', 'Suriname', 'Sweden', 'SyrianArabRepublic', 'Taiwan', 'Tajikistan', 'Tanzania', 'Thailand', 'Tonga', 'TrinidadandTobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Uganda', 'Ukraine', 'UnitedArabEmirates', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Venezuela', 'Vietnam', 'WallisandFutunaIslands', 'WesternSahara', 'Yemen', 'Zambia'] 164\n",
      "['SGD', 'USD', 'JPY', 'CAD', 'SZL', 'ARS', 'EUR', 'GBP', 'CHF', 'AUD', 'ush', 'ZAR', 'AFN', 'ALL', 'DZD', 'AOA', 'XCD', 'AMD', 'ANG', 'AZN', 'BSD', 'BHD', 'BDT', 'ush', 'BBD', 'BYR', 'BZD', 'XOF', 'BMD', 'BOB', 'BAM', 'BWP', 'BRL', 'BND', 'BGN', 'BIF', 'KHR', 'XAF', 'CVE', 'KYD', 'CLP', 'COP', 'KMF', 'CDF', 'CRC', 'HRK', 'CUP', 'CZK', 'DKK', 'DJF', 'DOP', 'IDR', 'EGP', 'SVC', 'ETB', 'EEK', 'ETB', 'FKP', 'DKK', 'FJD', 'XPF', 'GMD', 'GEL', 'GHS', 'GIP', 'DKK', 'GTQ', 'GGP', 'GNF', 'GYD', 'HTG', 'HNL', 'HKD', 'HUF', 'ISK', 'INR', 'IDR', 'IRR', 'IQD', 'ILS', 'JMD', 'JOD', 'KZT', 'KES', 'KPW', 'KRW', 'KWD', 'KGS', 'LAK', 'LVL', 'LBP', 'LSL', 'LRD', 'LYD', 'LTL', 'MOP', 'MKD', 'MGA', 'MWK', 'MYR', 'MVR', 'MRO', 'MUR', 'MXN', 'MDL', 'MNT', 'MAD', 'MZN', 'MMK', 'NAD', 'NPR', 'ANG', 'XPF', 'NZD', 'NIO', 'NGN', 'NOK', 'OMR', 'PKR', 'JOD', 'PAB', 'PGK', 'PYG', 'PEN', 'PHP', 'PLN', 'QAR', 'RON', 'RUB', 'RWF', 'ANG', 'STD', 'SAR', 'RSD', 'SCR', 'SLL', 'SBD', 'SOS', 'LKR', 'SDG', 'SRD', 'SEK', 'SYP', 'TWD', 'TJS', 'TZS', 'THB', 'TOP', 'TTD', 'TND', 'TRY', 'TMT', 'UGX', 'UAH', 'AED', 'UYU', 'UZS', 'VUV', 'VEF', 'VND', 'XPF', 'MAD', 'YER', 'ZMK'] 164\n"
     ]
    }
   ],
   "source": [
    "pattern_3 = re.compile(\"[a-zA-Z]\")\n",
    "lst_国家英文名=[]\n",
    "lst_货币英文缩写=[]\n",
    "for 汇率信息_i in sr_汇率信息:\n",
    "    lst_英文字符=re.findall(pattern_3,汇率信息_i)\n",
    "    str_英文字符串_i=''.join(lst_英文字符)\n",
    "    lst_国家英文名.append(str_英文字符串_i[:-3])\n",
    "    lst_货币英文缩写.append(str_英文字符串_i[-3:])\n",
    "print(lst_国家英文名,len(lst_国家英文名))\n",
    "print(lst_货币英文缩写,len(lst_货币英文缩写))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dbce2b1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>货币中文名</th>\n",
       "      <th>货币英文缩写</th>\n",
       "      <th>国家英文名</th>\n",
       "      <th>汇率</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>新币</td>\n",
       "      <td>SGD</td>\n",
       "      <td>Singapore</td>\n",
       "      <td>0.182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美元</td>\n",
       "      <td>USD</td>\n",
       "      <td>UnitedStates</td>\n",
       "      <td>0.140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日元</td>\n",
       "      <td>JPY</td>\n",
       "      <td>Japan</td>\n",
       "      <td>21.220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>加币</td>\n",
       "      <td>CAD</td>\n",
       "      <td>Canada</td>\n",
       "      <td>0.197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>斯威士兰里兰吉尼</td>\n",
       "      <td>SZL</td>\n",
       "      <td>Swaziland</td>\n",
       "      <td>2.410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>159</th>\n",
       "      <td>越南盾</td>\n",
       "      <td>VND</td>\n",
       "      <td>Vietnam</td>\n",
       "      <td>697.310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>160</th>\n",
       "      <td>太平洋法郎</td>\n",
       "      <td>XPF</td>\n",
       "      <td>WallisandFutunaIslands</td>\n",
       "      <td>14.410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>161</th>\n",
       "      <td></td>\n",
       "      <td>MAD</td>\n",
       "      <td>WesternSahara</td>\n",
       "      <td>1.280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>162</th>\n",
       "      <td>也门里亚尔</td>\n",
       "      <td>YER</td>\n",
       "      <td>Yemen</td>\n",
       "      <td>33.540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>163</th>\n",
       "      <td>赞比亚克瓦查</td>\n",
       "      <td>ZMK</td>\n",
       "      <td>Zambia</td>\n",
       "      <td>263.490</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>164 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        货币中文名 货币英文缩写                   国家英文名       汇率\n",
       "0          新币    SGD               Singapore    0.182\n",
       "1          美元    USD            UnitedStates    0.140\n",
       "2          日元    JPY                   Japan   21.220\n",
       "3          加币    CAD                  Canada    0.197\n",
       "4    斯威士兰里兰吉尼    SZL               Swaziland    2.410\n",
       "..        ...    ...                     ...      ...\n",
       "159       越南盾    VND                 Vietnam  697.310\n",
       "160     太平洋法郎    XPF  WallisandFutunaIslands   14.410\n",
       "161              MAD           WesternSahara    1.280\n",
       "162     也门里亚尔    YER                   Yemen   33.540\n",
       "163    赞比亚克瓦查    ZMK                  Zambia  263.490\n",
       "\n",
       "[164 rows x 4 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(4) 创建DataFrame，列标签：“货币中文名”“国家英文名”、“货币英文缩写”、“汇率”\n",
    "#行标签： 自然数序号\n",
    "dict_汇率信息={\"货币中文名\":lst_货币中文名, \"货币英文缩写\":lst_货币英文缩写,\n",
    "           \"国家英文名\":lst_国家英文名,\"汇率\":lst_汇率          }\n",
    "df_汇率信息表=pd.DataFrame(dict_汇率信息)\n",
    "df_汇率信息表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b14355c4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mining",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
